<template>
  <base-panel :show="visible" :width="panelWidth" @close="hide()" :re="true">
    <h3 slot="title">
      <span>{{$t('component.advancedSearch.title')}}</span>
      <el-dropdown
        class="pull-right"
        trigger="click"
        @command="setAdvanceSearchColumn"
      >
        <i
          class="iconfont icon-xitongguanli customer-panel-btn"
          style="float: none;"
        ></i>

        <el-dropdown-menu slot="dropdown" class="customer-advance-setting">
          <el-dropdown-item command="1">{{$t('common.base.table.oneCl')}}</el-dropdown-item>
          <el-dropdown-item command="2">{{$t('common.base.table.twoCl')}}</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </h3>
    <!--  -->
    <div id="v-task-step"></div>
    <div class="task-search-panel-title task-pointer task-flex task-ai" @click="show =!show">
      <span class="task-font16" id="v-task-step-6-dom">{{$t('task.components.list.commonSearchConditions')}}</span>
      <span slot="reference" class="task-font14 task-c2 task-ml12 task-mr4" @click.stop="$refs.taskSearchPupal.open()">{{$t('common.base.setting')}}</span>
      <span class="task-span1">
        <el-tooltip :content="$t('task.tip.searchPanelTip1')" placement="top">
          <i class="iconfont icon-question task-icon icon-color"></i>
        </el-tooltip>
      </span>
      <i class="iconfont icon-triangle-down task-f12 task-c9" v-if="!show"></i>
      <i class="iconfont icon-up task-icon icon-color" v-else></i>
    </div>
    <div class="task-search-guide" v-show="!fields.length && guide">
      <div></div>
      <div>
        {{$t('task.tip.searchPanelTip2')}}
      </div>
    </div>
    <!-- S 搜索条件 -->
    <el-form label-position="top" class="advanced-search-form task-search" onsubmit="return false;">
      <task-search-form
        v-show="show"
        class="task-search-forms"
        :fields="fields"
        ref="searchForm"
        :search-params="searchParams"
        :form-backup="formBackup"
        :column-num="columnNum"
      >
      </task-search-form>
      <div style="position: relative" >
        <div class="task-pointer task-flex task-ai">
          <span class="task-font16 task-mr4" id="v-task-step-7-dom">{{$t('task.components.list.addSearchConditions')}}</span>
          <span>
            <el-tooltip :content="$t('task.tip.searchPanelTip3')" placement="top">
              <i class="iconfont icon-question task-icon icon-color"></i>
            </el-tooltip>
          </span>
        </div>
        
      </div>
      <!-- 设置查询条件 -->
      <task-inquire 
        ref="taskInquireParams" 
        :column-num="columnNum" 
        :inquire-form-backup="inquireFormBackup"
        :config="inquireConfig" 
        @setting="_setting"
      />
      <!-- 搜索操作按钮 -->
      <slot name="footer"></slot>
    </el-form>
    <!-- E 搜索条件 -->
    <!-- E 搜索条件 -->
    <!-- 设置弹框 -->
    <task-search-pupal 
      ref="taskSearchPupal" 
      :config="config" 
      :search-field-storage-key="searchFieldStorageKey"
      :task-type-filter-fields="taskTypeFilterFields"
      :common-fields="commonFields"
      :task-inquire-list="taskInquireList"
      @taskPupal="_taskPupal"
    />
  </base-panel>
</template>

<script>
/* api */

import Vue from 'vue';

/* components */
import TaskSearchForm from './TaskSearchForm.vue';
import TaskSearchPupal from './TaskSearchPupal';
import TaskInquire from './TaskInquire';

import guideCompoment from '@src/component/guide/Guide';

let guideCompoments = Vue.extend(guideCompoment);

/* utils */
import _ from 'lodash';
import { formatDate } from 'pub-bbx-utils';
import { isEmptyStringObject } from '@src/util/function';
import { storageGet, storageSet } from '@src/util/storage';
import Log from '@src/util/log.ts'
import { isUndefined, isNull } from '@src/util/type'
import { parseObject } from '@src/util/lang/object.ts'
/* enum */
import TaskStateEnum from '@model/enum/TaskStateEnum.ts';
import StorageKeyEnum from '@model/enum/StorageKeyEnum.ts'
/* constants */
import {
  AllotTypeConvertMap,
  FlagConvertMap,
  TaskOnceConvertMap,
  TaskApproveConvertMap,
} from '@src/modules/task/model/TaskConvertMap.ts';
/* service */
import { isUserField } from '@service/FieldService.ts'
import { isBasicEditionHidePay, isBasicEditionHideEvent, isBasicEditionHidePlanWork, isBasicEditionHideApi } from '@shb-lib/version';

const TASK_HISTORY_KEY = 'task_history_list';
const MultiFieldNames = [
  'serviceType',
  'serviceContent',
  'level',
  'paymentMethod',
  'state',
  'allotTypeStr',
  'onceException',
  'paymentMethod',
  'tag',
];
const TaskInquireConvertFieldNamesToConditionsMap = {
  customer: 'customerId',
  product: 'productId',
  tlmName: 'tlmId',
};

import { TASK_GUIDE_SEARCH_MODEL } from '@src/component/guide/taskV2Store';

export default {
  name: 'task-search-panel',
  props: {
    customizeList: {
      type: Array,
      default: () => [],
    },
    config: {
      type: Array,
      default: () => [],
    },
    initData: {
      type: Object,
      default: () => ({}),
    },
    searchParams: {
      type: Object,
      default: () => [],
    },
    searchFieldStorageKey: {
      type: String,
      default: StorageKeyEnum.TaskListSearchFields
    }
  },
  watch: {
    customizeList() {
      this.taskTypeFilterFields;
      this.commonFields;
      this._taskInquireList();
    },
    selfFields() {
      this.fields;
    },
    config() {
      this.updateTaskType();
    },
  },
  data() {
    return {
      columnNum: 1,
      formBackup: {},
      inquireFormBackup: {},
      selfFields: [],
      taskInquireList: [],
      visible: false,
      show: true,
      guide: true,
      taskAllFields: [],
      taskAllFieldsMap: {},
    };
  },
  computed: {
    taskTypeFilterFields() {
      let { customizeList } = this;
      let taskTypeFilterFields = customizeList.filter((item) => {
        return item.isSystem == 0 && item.isSearch && item.isCommon==0;
      });
      return taskTypeFilterFields;
    },
    commonFields(){
      let { customizeList } = this;
      let commonFields = customizeList.filter((item) => {
        return item.isSystem == 0 && item.isSearch && item.isCommon==1;
      });
      return commonFields;
    },
    fields() {
      let f = {};
      let selfFields = [];
      let fields = [...this.selfFields]
        .filter((item) => {
          let bool = [...this.taskTypeFilterFields, ...this.config,...this.commonFields].some(
            (v) => {
              return item.fieldName === v.fieldName;
            }
          );
          if (bool) return item;
        })
        .map((field) => {
          f = _.cloneDeep(field);
          let formType = f.formType;

          if (formType === 'datetime') {
            formType = 'date';
          }

          if (formType === 'updateTime') {
            f.displayName = this.$t('task.list.displayName.updateTime');
          }
          return {
            ...f,
            isNull: 1,
            formType,
            originalFormType: f.formType,
            operator: this.matchOperator(f),
          };
        });
      // .sort((a, b) => a.orderId - b.orderId);

      fields.forEach((field) => {
        let { fieldName } = field;
        let originField = this.taskAllFieldsMap[fieldName];

        selfFields.push(originField ? originField : field);
      });
      return selfFields;
    },
    // 查询条件字段
    inquireConfig() {
      return [...this.config, ...this.taskTypeFilterFields,...this.commonFields].filter(item => {
        // 查询条件过滤掉工单类型筛选项 后端暂不支持 相关bug 10104
        return item.fieldName !== 'taskType'
      })
    },
    panelWidth() {
      return `${420 * this.columnNum}px`;
    },
    // 基础版功能是否隐藏支付
    isBasicEditionHidePay() {
      return isBasicEditionHidePay() 
    },
    // 基础版功能是否隐藏事件
    isBasicEditionHideEvent() {
      return isBasicEditionHideEvent() 
    },
    // 基础版功能是否隐藏计划任务
    isBasicEditionHidePlanWork() {
      return isBasicEditionHidePlanWork() 
    },
    // 基础版功能是否隐藏api
    isBasicEditionHideApi() {
      return isBasicEditionHideApi() 
    }
  },
  mounted() {
    this._selfFields();
  },
  methods: {
    isTaskListModule() {
      return this.searchFieldStorageKey == StorageKeyEnum.TaskListSearchFields
    },
    setFormUserFieldsToMultiple(storageSearchFields = []) {
      return (
        storageSearchFields.map(field => {
          // 用户字段支持多选
          if (isUserField(field)) {
            field.setting = {
              ...(field.setting || {}),
              isMulti: true
            }
          }
          return field
        })
      )
    },
    _selfFields() {
      const { column_number } = this.getLocalStorageData();
      const searchField = localStorage.getItem(this.searchFieldStorageKey);

      if (column_number) this.columnNum = Number(column_number);

      if (!searchField) {
        this.selfFields = [];
      } else {
        let searchFields = parseObject(searchField, {})
        searchFields = searchFields?.list || []
        // 工单列表需要兼用旧缓存数据，特此处理，修改用户字段为多选
        this.selfFields = this.isTaskListModule() ? this.setFormUserFieldsToMultiple(searchFields) : searchFields
      }
      
      // 基础版创建方式过滤由事件创建字段、工单状态过滤已结算字段
      if (this.isBasicEditionHideEvent) {
        this.selfFields.forEach(item => {
          if (item.fieldName === 'source') {
            // TODO 国际化待办
            item.setting.dataSource = item.setting?.dataSource.filter(source => source !== '由事件创建')
          }
        })
      }
      if (this.isBasicEditionHidePay) {
        this.selfFields.forEach(item => {
          if (item.fieldName === 'state') {
            item.setting.dataSource = item.setting?.dataSource.filter(source => source !== '已结算')
          }
        })
      }
      if (this.isBasicEditionHidePlanWork) {
        // 基础版创建方式过滤计划任务创建字段
        this.selfFields.forEach(item => {
          if (item.fieldName === 'source') {
            item.setting.dataSource = item.setting?.dataSource.filter(source => source !== '计划任务创建')
          }
        })
      }
      if (this.isBasicEditionHideApi) {
        // 基础版创建方式过滤API创建字段
        this.selfFields.forEach(item => {
          if (item.fieldName === 'source') {
            item.setting.dataSource = item.setting?.dataSource.filter(source => source !== 'API创建')
          }
        })
      }
      this._taskInquireList();
    },
    buildParams() {
      // 判断是否有重复选择
      let searchFormData = this.$refs.searchForm.returnData();
      let taskInquireFormData = this.$refs.taskInquireParams.returnData();
      let inPar = [];
      let repeatBool;


      Log.info(searchFormData, 'searchFormData', this.buildParams.name)

      for (let key in searchFormData) {
        if (
          JSON.stringify(searchFormData[key]) !== '[]'
          && searchFormData[key]
          && key !== 'backUp'
        ) {
          if (key !== 'area') {
            inPar.push(key);
          } else {
            if (
              JSON.stringify(searchFormData[key]) !== '{}'
              && searchFormData[key].province
            ) {
              inPar.push('area');
            }
          }
        }
      }
      for (let key in taskInquireFormData) {
        if (
          inPar.includes(key)
          && taskInquireFormData[key]
          && JSON.stringify(taskInquireFormData[key]) !== '[]'
          && key !== 'backUp'
        ) {
          if (key !== 'customer' && key !== 'tags' && key !== 'area') {
            repeatBool = true;
          } else {
            if (
              taskInquireFormData['area']
              && taskInquireFormData['area'].province
            ) {
              repeatBool = true;
            }
            if (
              taskInquireFormData['tags']
              && taskInquireFormData['tags'].length
            ) {
              repeatBool = true;
            }
            if (taskInquireFormData['customer']) {
              repeatBool = true;
            }
          }
        }
      }

      const form = {
        // ...taskInquireFormData, // todo 这里先注释 不处理添加的处理条件 在下面已经有处理了，后面再看看怎么重构这玩意
        ...searchFormData,
      };
      this.formBackup = Object.assign({}, searchFormData);
      this.inquireFormBackup = Object.assign(
        {},
        taskInquireFormData
      );

      const taskInquireList = this.taskInquireList.length
        ? this.taskInquireList
        : [...this.config, ...this.taskTypeFilterFields, ...this.commonFields];

      const isSystemFields = [...this.fields, ...taskInquireList].filter(
        (f) => f.isSystem
      );
      const notSystemFields = [...this.fields, ...taskInquireList].filter(
        (f) => !f.isSystem
      );
      let params = {
        conditions: [],
        balanceConditions: [],
        faultLibraryInput: {} // 故障库
      };
      let tv = null;
      let fn = '';
      let fieldValue = null
      // 固定条件
      for (let i = 0; i < isSystemFields.length; i++) {
        tv = isSystemFields[i];
        fn = tv.fieldName;
        fieldValue = form[fn]

        // hasRemind
        if (fn === 'hasRemind' && form[fn] !== '') {
          params.hasRemind = form[fn] == 2 ? 0 : form[fn];
          continue;
        }

        if (fn === 'qrcodeState' && form[fn] !== '') {
          params.qrcodeState = form[fn] == 2 ? 0 : form[fn];
          continue;
        }

        if (fn == 'area' && form[fn]) {
          params.productAddress = {
            ...(params.productAddress || {}),
            province: form[fn].province,
            city: form[fn].city,
            dist: form[fn].dist,
          };
          continue;
        }

        if (fn === 'addressDetail') {
          params.productAddress = {
            ...(params.productAddress || {}),
            address: form[fn],
          };
          continue;
        }

        // start 故障库
        if (fn === 'faultReason') {
          params.faultLibraryInput.faultReason = form[fn]
          continue;
        }
        if (fn === 'faultScene') {
          params.faultLibraryInput.faultScene = form[fn]
          continue;
        }
        if (fn === 'productType') {
          const productTypeId = form[fn] ? form[fn][form[fn].length - 1] : ''
          params.faultLibraryInput.productTypeId = productTypeId
          continue;
        }
        // end 故障库
        
        if (isUndefined(fieldValue) || isNull(fieldValue) || fieldValue === '' || (Array.isArray(fieldValue) && !fieldValue.length)) {
          continue;
        }

        if (typeof form[fn] === 'string') {
          let fieldNamsMap = { customer: 'customerId', product: 'productId' };

          params[fieldNamsMap[fn] ? fieldNamsMap[fn] : fn] = form[fn];
          continue;
        }

        if (['date', 'datetime'].includes(tv.formType)) {
          params[`${fn}Start`] = form[fn][0];
          params[`${fn}End`] = form[fn][1];
          continue;
        }

        if (MultiFieldNames.indexOf(fn) > -1) {
          params[`${fn}s`] = form[fn];
          delete params[fn];
          continue;
        }

        if (tv.fieldName === 'tags') {
          params.tagId = form[fn].map(({ id }) => id).join('');
        }

        params[fn] = form[fn];
      }
      // 自定义条件
      for (let i = 0; i < notSystemFields.length; i++) {
        let key = null;
        tv = notSystemFields[i];
        fn = tv.fieldName;
        !tv.operator ? (tv['operator'] = this.matchOperator(tv)) : '';
        const valueKey = Array.isArray(form[fn]) ? 'inValue' : 'value';
        if (!form[fn] || (Array.isArray(form[fn]) && !form[fn].length)) {
          continue;
        }

        if (tv.isBalanceField === true) {
          params.balanceConditions.push({
            property: fn,
            operator: tv.formType === 'select' ? 'in' : tv.operator,
            [valueKey]: form[fn],
            key
          });
          continue;
        }

        // 空对象
        if (
          typeof form[fn] === 'object'
          && !Array.isArray(form[fn])
          && !Object.keys(form[fn]).length
        ) {
          continue;
        }

        if (['date', 'datetime'].includes(tv.originalFormType)) {
          params.conditions.push({
            property: fn,
            operator: tv.operator,
            betweenValue1: form[fn][0],
            betweenValue2: form[fn][1],
          });
          continue;
        }

        if (tv.formType === 'cascader') {
          const _condition = {
            property: fn,
            operator: tv.setting?.isMulti ? 'multiSelect' : tv.operator,
            inValue: form[fn],
          }
          params.conditions.push(_condition);
          continue;
        }

        if (tv.fieldName === 'isRepeatRepair') {
          params.conditions.push({
            property: fn,
            operator: 'in',
            inValue: form[fn].map(item => {
              if (item === '全部' || item === this.$t('common.base.all')) item = ''
              return item
            }),
          });
          continue;
        }

        if (tv.formType === 'user' && Array.isArray(form[fn])) {
          params.conditions.push({
            property: fn,
            operator: 'user',
            [valueKey]: form[fn],
          });
          continue;
        }

        if (tv.formType === 'select' && !tv.setting.isMulti && !tv.isSystem) {
          params.conditions.push({
            property: fn,
            operator: 'in',
            [valueKey]: form[fn],
          });
          continue;
        }

        if (tv.formType === 'related_task') {
          key = 'taskNo';
        }
        
        if (tv.isBalanceField !== true) {
          params.conditions.push({
            property: fn,
            operator: tv.operator,
            [valueKey]: form[fn],
            key
          });
        }
      }

      this.buildTaskInquireParams(params);

      // 返回接口数据
      if (params.conditions) {
        params.conditions = [...new Set(
          params.conditions.map((item) => {
            item = JSON.stringify(item);
            return item;
          })
        )].map((item) => {
          item = JSON.parse(item);
          return item;
        });
      } 
      this.changeParamsValue(params);
      return { params: this.clearParams(params), repeatBool };
    },
    buildTaskInquireParams(params) {
      const taskInquireList = this.$refs.taskInquireParams.returnInquireFields();
      const form = this.$refs.taskInquireParams.returnData();
      this.formBackup = Object.assign(this.formBackup, {
        ...this.$refs.searchForm.returnData(),
      });
      this.inquireFormBackup = Object.assign(this.inquireFormBackup, form);

      const isSystemFields = taskInquireList.filter((f) => f.isSystem);
      const notSystemFields = taskInquireList.filter((f) => !f.isSystem);
      const abnormals = ['refusedReason', 'pausedReason', 'rollbackReason', 'reallotReason', 'offedReason']

      params.systemConditions = [];

      let tv = null;
      let fn = '';
      // 固定条件
      for (let i = 0; i < isSystemFields.length; i++) {
        tv = isSystemFields[i];
        fn = tv.fieldName;
        const valueKey = Array.isArray(form[fn]) ? 'inValue' : 'value';
        if (!form[fn] || (Array.isArray(form[fn]) && !form[fn].length)) {
          continue;
        }

        // 空对象
        if (
          typeof form[fn] === 'object'
          && !Array.isArray(form[fn])
          && !Object.keys(form[fn]).length
        ) {
          continue;
        }

        if (tv.formType === 'address') {
          let address = [];
          let isEmpty = isEmptyStringObject(form[fn]);

          if (!isEmpty) {
            if (form[fn].province) {
              address.push({
                property: 'province',
                operator: tv.operatorValue,
                value: form[fn].province,
              });
            }
            if (form[fn].city) {
              address.push({
                property: 'city',
                operator: tv.operatorValue,
                value: form[fn].city,
              });
            }
            if (form[fn].dist) {
              address.push({
                property: 'dist',
                operator: tv.operatorValue,
                value: form[fn].dist,
              });
            }
          }
          params.systemConditions = [...params.systemConditions, ...address];
          continue;
        }

        if (tv.fieldName == 'tags') {
          let condition = {
            property: 'tagIds',
            operator: tv.operatorValue,
            inValue: form[fn].map((tag) => {
              return tag.id;
            }),
          };
          params.systemConditions.push(condition);
          continue;
        }

        if (tv.fieldName == 'state') {
          let condition = {
            property: fn,
            operator: tv.operatorValue,
            inValue: form[fn].map((stateName) =>
              TaskStateEnum.getValue(stateName)
            ),
          };
          params.systemConditions.push(condition);
          continue;
        }

        if (abnormals.indexOf(tv.fieldName) !== -1) {
          let condition = {
            property: tv.englishName,
            operator: tv.operatorValue,
            inValue: form[fn],
          };
          params.systemConditions.push(condition);
          continue;    
        }

        if (tv.fieldName === 'exceptionType') {
          let exceptionType;
          switch (form[fn]) {
          case this.$t('common.task.button.pause'):
            exceptionType = 1;
            break;
          case this.$t('common.task.exceptionStatus.overTime'):
            exceptionType = 2;
            break;
          default:
            exceptionType = 0;
            break;
          }
          params.systemConditions.push({
            property: 'exceptionType',
            operator: tv.operatorValue,
            value: exceptionType,
          });
          continue;
        }

        if (tv.fieldName == 'product') {
          params.systemConditions.push({
            property: 'productId',
            operator: tv.operatorValue,
            value: form[fn],
          });
          continue;
        }

        if (tv.fieldName === 'paymentMethod') {
          params.conditions.push({
            property: fn,
            operator: tv.operatorValue,
            inValue: form[fn],
          });
          continue;
        }

        if (tv.fieldName == 'allotTypeStr') {
          params.systemConditions.push({
            property: 'allotType',
            operator: tv.operatorValue,
            inValue: form[fn].map((type) => AllotTypeConvertMap[type]),
          });
          continue;
        }

        if (tv.fieldName == 'onceException') {
          params.systemConditions.push({
            property: 'flags',
            operator: tv.operatorValue,
            inValue: form[fn].map(
              (exception) => FlagConvertMap[exception] || ''
            ),
          });
          continue;
        }

        if (tv.fieldName === 'synergyId') {
          let condition = {
            property: 'synergies',
            operator: tv.operatorValue,
            inValue: form[fn],
          };
          params.systemConditions.push(condition);
          continue;
        }

        // if (tv.fieldName === "source") {
        //   let condition = {
        //     property: "source",
        //     operator: tv.operatorValue,
        //     inValue: form[fn],
        //   };
        //   params.systemConditions.push(condition);
        //   continue;
        // }

        if (['date', 'datetime', 'planStartTime', 'planEndTime'].includes(tv.formType)) {
          params.systemConditions.push({
            property: fn,
            operator: tv.operatorValue,
            betweenValue1: form[fn][0],
            betweenValue2: form[fn][1],
          });
          continue;
        }

        if (tv.fieldName === 'executor') {
          params.systemConditions.push({
            property: 'executorUser',
            operator: tv.operatorValue,
            inValue: form[fn],
          });
          continue;
        }

        if (
          MultiFieldNames.indexOf(tv.formType) !== -1
          || tv.formType === 'user'
        ) {
          params.systemConditions.push({
            property: fn,
            operator: tv.operatorValue,
            inValue: form[fn],
          });
          continue;
        }

        if (TaskInquireConvertFieldNamesToConditionsMap[fn]) {
          params.systemConditions.push({
            property: TaskInquireConvertFieldNamesToConditionsMap[fn],
            operator: tv.operatorValue,
            value: form[fn],
          });
          continue;
        }

        let value = TaskOnceConvertMap[form[fn]] != undefined
          ? TaskOnceConvertMap[form[fn]]
          : form[fn];
        value = TaskApproveConvertMap[value] != undefined
          ? TaskApproveConvertMap[value]
          : value;

        params.systemConditions.push({
          property: fn,
          operator: tv.operatorValue,
          [valueKey]: value,
        });
      }

      // 自定义条件
      for (let i = 0; i < notSystemFields.length; i++) {
        let key = null;

        tv = notSystemFields[i];
        fn = tv.fieldName;
        const valueKey = Array.isArray(form[fn]) ? 'inValue' : 'value';
        // !tv.operatorValue ? (tv['operatorValue'] = this.matchOperator(tv)) : '';
        if (!form[fn] || (Array.isArray(form[fn]) && !form[fn].length)) {
          continue;
        }

        // 空对象
        if (
          typeof form[fn] === 'object'
          && !Array.isArray(form[fn])
          && !Object.keys(form[fn]).length
        ) {
          continue;
        }

        if (tv.fieldName === 'isRepeatRepair') {
          params.conditions.push({
            property: fn,
            operator: tv.operatorValue,
            inValue: form[fn].map(item => {
              if (item === '全部' || item === this.$t('common.base.all')) item = ''
              return item
            }),
          });
          continue;
        }

        // FIXME: 同下面 datetime
        if (tv.formType === 'date') {
          params.conditions.push({
            property: fn,
            operator: tv.operatorValue,
            betweenValue1: formatDate(form[fn][0], 'YYYY-MM-DD HH:mm:ss'),
            betweenValue2: `${formatDate(form[fn][1], 'YYYY-MM-DD')} 23:59:59`,
          });
          continue;
        }

        if (tv.formType === 'cascader') {
          const _condition = {
            property: fn,
            operator: tv.setting?.isMulti ? 'multiSelect' : tv.operator,
            inValue: form[fn],
          }
          params.conditions.push(_condition);
          continue;
        }
        if (tv.formType === 'user' && Array.isArray(form[fn])) {
          params.conditions.push({
            property: fn,
            operator: 'user',
            inValue: form[fn],
          });
          continue;
        }

        if (tv.formType === 'select' && !tv.setting.isMulti && !tv.isSystem && !tv.isBalanceField) {
          params.conditions.push({
            property: fn,
            operator: tv.operatorValue,
            [valueKey]: form[fn],
          });
          continue;
        }
        
        if (tv.formType === 'select' && !tv.isSystem && tv.isBalanceField) {
          if (!params.balanceConditions) {
            continue
          }
          
          params.balanceConditions = params.balanceConditions.filter(item => item.property != fn)
          
          params.balanceConditions.push({
            property: fn,
            operator: tv.operatorValue,
            inValue: form[fn],
          });
          continue;
        }
        
        // FIXME: 这里 form[fn] 为 字 符串的时候 error
        if (tv.formType === 'datetime') {
          params.conditions.push({
            property: fn,
            operator: tv.operatorValue,
            betweenValue1: formatDate(form[fn][0], 'YYYY-MM-DD HH:mm:ss'),
            betweenValue2: `${formatDate(form[fn][1], 'YYYY-MM-DD')} 23:59:59`,
          });
          continue;
        }

        if(tv.formType === 'related_task') {
          key = 'taskNo';
        }

        if (tv.isBalanceField !== true) {
          let repeatIndex = params.conditions.findIndex(item => item.property == fn)

          if(repeatIndex >= 0){
            params.conditions.splice(repeatIndex, 1)
          }
          
          params.conditions.push({
            key,
            property: fn,
            operator: tv.operatorValue,
            [valueKey]: form[fn],
          });
        }
      }
    },
    // 当parms.conditions里面operator === 'in' || operator === 'not_in'时，value 应该是inValue, 传的值也要是个数组
    changeParamsValue(params){
      if (params.conditions) {
        params.conditions = params.conditions.map((item) => {
          if (item.operator === 'in' || item.operator === 'not_in') {
            if(item.value){
              item.inValue = item.value
              delete item.value
            }

            if(!Array.isArray(item.inValue)){
              item.inValue = [item.inValue]
            }
          }
          return item;
        });
      }
    },
    /**
     * 添加查询条件存在就删除常用条件
     */
    clearParams(params) {
      if (params.systemConditions) {
        params.systemConditions.forEach((item) => {
          if (item.property === 'serviceContent') {
            params.serviceContents = [];
          } else if (item.property === 'serviceType') {
            params.serviceTypes = [];
          } else if (item.property === 'level') {
            params.levels = [];
          } else if (item.property === 'state') {
            params.states = [];
          } else if (item.property === 'allotType') {
            params.allotTypeStrs = [];
          } else if (item.property === 'flags') {
            params.onceExceptions = [];
          } else if (item.property === 'allotUser') {
            params.allotUser = [];
          } else if (item.property === 'createUser') {
            params.createUser = [];
          } else if (item.property === 'executorUser') {
            params.executor = [];
          } else if (item.property === 'synergies') {
            params.synergyId = [];
          } else if (item.property === 'tagIds') {
            params.searchTagIds = [];
          } else {
            params[item.property] = '';
          }
        });
      }
      return params;
    },
    getLocalStorageData() {
      const dataStr = storageGet(TASK_HISTORY_KEY, '{}');
      return JSON.parse(dataStr);
    },
    matchOperator(field) {
      let formType = field.formType;
      let operator = '';

      switch (formType) {
      case 'date': {
        operator = 'between';
        break;
      }
      case 'datetime': {
        operator = 'between';
        break;
      }
      case 'planStartTime': {
        operator = 'between';
        break;
      }
      case 'planEndTime': {
        operator = 'between';
        break;
      }
      case 'select': {
        if (field.setting && field.setting.isMulti) {
          operator = 'in';
        } else {
          operator = 'eq';
        }
        break;
      }
      case 'user': {
        operator = 'user';
        break;
      }
      case 'cascader': {
        operator = 'cascader';
        break;
      }
      case 'address': {
        operator = 'address';
        break;
      }
      case 'location': {
        operator = 'location';
        break;
      }
      case 'related_task': {
        operator = 'array_eq';
        break;
      }
      case 'formula': {
        operator = 'eq';
        break;
      }
      default: {
        operator = 'like';
        break;
      }
      }
      return operator;
    },
    open() {
      this.visible = true;
    },
    hide() {
      this.visible = false;
      this.$emit('bj', false);
    },
    resetParams() {
      this.formBackup = {};
      this.inquireFormBackup = {};
      this.$refs.searchForm
        && this.$nextTick(this.$refs.searchForm.initFormVal);
      this.$refs.taskInquireParams
        && this.$nextTick(this.$refs.taskInquireParams.initFormVal);
    },
    saveDataToStorage(key, value) {
      const data = this.getLocalStorageData();
      data[key] = value;

      storageSet(TASK_HISTORY_KEY, JSON.stringify(data));
    },
    setAdvanceSearchColumn(command) {
      this.columnNum = Number(command);
      this.saveDataToStorage('column_number', command);
    },
    _taskPupal({ list, checkSystemList, checkCustomizeList ,checkComList}) {
      this.selfFields = list;

      this._taskInquireList(
        JSON.stringify({ checkSystemList, checkCustomizeList ,checkComList})
      );

      this.$nextTick(() => {
        this.mergeTaskFieldsForTaskInquire();
      });
    },
    _taskInquireList(field = '') {
      const searchField = field || localStorage.getItem(this.searchFieldStorageKey);
      if (searchField) {
        this.taskInquireList = [
          ...this.config,
          ...this.taskTypeFilterFields,
          ...this.commonFields
        ].filter((item, index) => {
          let bool = [
            ...JSON.parse(searchField).checkSystemList,
            ...JSON.parse(searchField).checkCustomizeList,
            ...JSON.parse(searchField).checkComList
          ].some((v) => {
            return v === item.fieldName;
          });
          if (!bool) {
            return item;
          }
        });
      }
    },
    // 设置查询条件
    _setting({ list, check_system_list, check_customize_list,check_common_list }) {
      const searchField = localStorage.getItem(this.searchFieldStorageKey);
      let loc;
      [...this.config, ...this.taskTypeFilterFields,...this.commonFields].filter((value, index) => {
        let bool = list.some((v) => {
          return value.fieldName === v;
        });
        if (bool) {
          this.selfFields.push(value);
        }
      });
      this.selfFields = [
        ...new Set(
          this.selfFields.map((item) => {
            item = JSON.stringify(item);
            return item;
          })
        ),
      ].map((item) => {
        item = JSON.parse(item);
        return item;
      });
      // 设置查询条件的select字段
      if (searchField) {
        this.taskInquireList = [
          ...this.config,
          ...this.taskTypeFilterFields,
          ...this.commonFields
        ].filter((value, index) => {
          let bool = [
            ...JSON.parse(searchField).checkSystemList,
            ...JSON.parse(searchField).checkCustomizeList,
            ...JSON.parse(searchField).checkComList,
            ...list,
          ].some((v) => {
            return v === value.fieldName;
          });
          if (!bool) {
            return value;
          }
        });
        loc = {
          list: this.selfFields,
          checkSystemList: [
            ...JSON.parse(searchField).checkSystemList,
            ...check_system_list,
          ],
          checkCustomizeList: [
            ...JSON.parse(searchField).checkCustomizeList,
            ...check_customize_list,
          ],
         checkComList: [
            ...JSON.parse(searchField).checkComList,
            ...check_common_list,
          ],
        };
      } else {
        this.taskInquireList = [
          ...this.config,
          ...this.taskTypeFilterFields,
          ...this.commonFields
        ].filter((value, index) => {
          let bool = list.some((v) => {
            return v === value.fieldName;
          });
          if (!bool) {
            return value;
          }
        });
        loc = {
          list: this.selfFields,
          checkSystemList: [...check_system_list],
          checkCustomizeList: [...check_customize_list],
          checkComList: [...check_common_list],
        };
      }
      localStorage.setItem(this.searchFieldStorageKey, JSON.stringify(loc));
    },

    mergeTaskFields(taskAllFields = []) {
      // 临时这种用法
      this.taskAllFields = taskAllFields.slice();
      this.taskAllFieldsMap = taskAllFields.reduce(
        (acc, field) => (acc[field.fieldName] = field) && acc,
        {}
      );

      let selfFields = [];

      this.selfFields.forEach((field) => {
        let { fieldName } = field;
        let originField = this.taskAllFieldsMap[fieldName];

        selfFields.push(originField ? originField : field);
      });

      this.selfFields = selfFields.slice();

      this.$nextTick(() => {
        this.mergeTaskFieldsForTaskInquire();
      });
    },
    mergeTaskFieldsForTaskInquire() {
      let selfFields = [];

      this.taskInquireList.forEach((field) => {
        let { fieldName } = field;
        let originField = this.taskAllFieldsMap[fieldName];

        selfFields.push(originField ? originField : field);
      });
      this.taskInquireList = selfFields.slice();
    },
    createGuide( arr = []) {
      this.$Guide(arr, 0, TASK_GUIDE_SEARCH_MODEL).create().then(res_=>{if(res_)storageSet(TASK_GUIDE_SEARCH_MODEL, '2')})
    },
    // 更新工单类型下拉框数据
    updateTaskType() {
      for (let item of this.selfFields) {
        if(item.fieldName === 'taskType') {
          for(let jtem of this.config) {
            if (jtem.fieldName === 'taskType') {
              item.setting.dataSource = jtem.setting.dataSource;
              break;
            }
          }
          break;
        }
      }
    },
  },
  components: {
    [TaskSearchForm.name]: TaskSearchForm,
    [TaskSearchPupal.name]: TaskSearchPupal,
    [TaskInquire.name]: TaskInquire,
  },
};
</script>

<style lang="scss">
.task-search-forms {
  transition: height 0.5s;
  .form-item {
    width: 340px !important;
  }
}
.task-search{
  .form-item-control {
    input{
      width: 100%;
    }
  }
}
</style>
<style lang="scss" scoped>
.advanced-search-form {
  overflow: auto;
  padding: 10px 15px 300px 15px;

  height: calc(100%);
  justify-content: space-between;
  overflow-x: hidden;

  .two-columns {
    display: flex;
    flex-wrap: wrap;
    .el-form-item {
      width: 50%;
    }
  }

  .form-item-container {
    justify-content: space-between;
  }

  .form-item {
    label {
      padding-left: 0;
    }

    width: 390px;
  }

  .advanced-search-btn-group {
    display: flex;
    justify-content: flex-end;
    width: 100%;
    position: absolute;
    bottom: 0px;
    background: #fff;
    padding: 15px 20px;

    .base-button {
      margin: 0 10px;
    }
  }
}
.hide {
  overflow: hidden;
  padding: 0;
  height: 0;
  width: 0;
}
.task-search-panel-title {
  height: 54px;
  line-height: 54px;
  padding: 0 15px;
}
.task-search-guide {
  position: relative;
  left: 120px;
  margin-bottom: 20px;
  > div {
    &:first-child {
      width: 0;
      height: 0;
      border-left: 4px solid transparent;
      border-right: 4px solid transparent;
      border-bottom: 6px solid $color-primary-light-6;
      @include borderBottomColor();
      margin-left: 15px;
    }
    &:last-child {
      position: relative;
      width: 267px;
      height: 50px;
      font-size: 14px;
      color: #fff;
      line-height: 50px;
      @include backgroundColor();
      box-shadow: 0px 6px 16px 0px rgba(0, 0, 0, 0.08),
        0px 3px 6px -4px rgba(0, 0, 0, 0.12);
      text-align: center;
      border-radius: 4px;
      > span {
        font-size: 12px;
        font-family: fantasy;
        position: absolute;
        top: 8px;
        right: 9px;
        line-height: 10px;
      }
    }
  }
}
</style>
